#!/bin/bash
# chkconfig: 12345 07 91
# description: This calls the HMC restore scripts (prior to network set up)
#

# Source function library.
. /etc/init.d/functions

# Critical error messages. Keep them under 80 chars per line
ARCHIVE_ERR_MSG_0="A critical error occurred during the recovery of archive data."
ARCHIVE_ERR_MSG_1="The system may be unstable. It is recommended the HMC be"
ARCHIVE_ERR_MSG_2="re-installed and manually reconfigured.\n"
ARCHIVE_ERR_MSG_3="Contact your service representative to determine the severity"
ARCHIVE_ERR_MSG_4="of this condition if you choose to proceed. Press <Enter> to"
ARCHIVE_ERR_MSG_5="continue the boot sequence or press 'R', then <Enter> to"
ARCHIVE_ERR_MSG_6="re-install the HMC.\n"

REINSTALL_MSG="Insert the HMC recovery CD and press <Enter> to continue with re-installation."

NO_RESTORE_MSG_0="A non-critical error was encountered while recovering archive data."
NO_RESTORE_MSG_1="No archive files have been restored. Contact your service"
NO_RESTORE_MSG_2="representative for further analysis. Press <Enter> to continue"
NO_RESTORE_MSG_3="the boot sequence."

case "$1" in
    start)
 
        # Logging information
        LOGDIR=/var/hsc/log
        FINALLOG=$LOGDIR/hmcRestore.log
        LOG=$LOGDIR/hmcRestore.tmp
        LOG_ERROR_LOG=/tmp/hmcRestore.log
        
        # Check if the directory for the log file exists.
        if [ ! -d $LOGDIR ]; then
            echo "=================================================================" > $LOG_ERROR_LOG
            echo "HMC restore script processing log for `date`." >> $LOG_ERROR_LOG
            echo "HMC log directory, <$LOGDIR>, does not exist. Program exiting" >> $LOG_ERROR_LOG
            exit 1
        fi

        
        #-----------------------------------------------------------------------
        # Something to the screen while booting
        #-----------------------------------------------------------------------
        echo -n "HMC file archive processing in progress. Please wait."
        
        
        #-----------------------------------------------------------------------
        # Start log to HMC restore actions.
        #-----------------------------------------------------------------------
        if [ ! -f $LOG ]; then
           echo "--------- start log --------" > $LOG
        else
           echo "--------- log resumed after reboot --------" >> $LOG
        fi
        echo -e "HMC restore processing log for `date`.\n" >> $LOG
        
        
        #-----------------------------------------------------------------------
        # Due to a bug in the GA2 to GA3 Save Upgrade Data processing which
        # occurred when migrating from RedHat 7.1 to 7.2, we will ensure that
        # /etc/sysconfig/network-scripts/network-functions file gets preserved.
        # That file was erroneously saved and should NOT be restored between
        # the 2 different Linux releases. (Careful - the 'cp' command might be
        # aliased!)
        #-----------------------------------------------------------------------
        if [ -f /etc/sysconfig/network-scripts/network-functions ]; then
           /bin/cp -f /etc/sysconfig/network-scripts/network-functions /tmp/network-functions
           echo "network-functions script temporarily archived" >> $LOG
        fi
        
        
        #-----------------------------------------------------------------------
        # Query ccfw uid/gid and log this information prior to data recovery
        #-----------------------------------------------------------------------
        typeset -i ccfwpreexist
        ccfwpreexist=0
        userList=`cat /etc/passwd | cut -d':' -f 1`
        for i in $userList
        do
             if [ "$i" == "ccfw" ]; then
                  ccfwpreexist=1
                  echo "ccfw user exists prior to data restore" >> $LOG
             fi
        done
        
        # archive this information in case we have rebooted
        if [ ! -f /tmp/ccfw.data ]; then
             echo $ccfwpreexist > /tmp/ccfw.data
             chmod 777 /tmp/ccfw.data
             echo "archive of ccfw information complete" >> $LOG
        else
             echo "archive of ccfw information already exists" >> $LOG
        fi
        
        
        #-----------------------------------------------------------------------
        # Call the actual data restore processes
        #-----------------------------------------------------------------------
        /bin/sh /opt/hsc/bin/restore 2>/dev/null
        restoreRC=$?
        echo "system restore of backup critical data completed, rc=$restoreRC." >> $LOG
        
        case "$restoreRC" in 
            0) 
                 # Successful restore of backup data was done.  Force a warm 
                 # reboot since we do not know what, if any, critical files 
                 # may have been copied back onto the system that was just 
                 # re-loaded.
                 #
                 # An rc=0 indicated that an actual restore was done. Rc=12 
                 # states that we attampted to do a restore, but none was 
                 # necessary. In between rc's are various errors.
                 #
                 # Use 'reboot' since it's unclear whether or not doing a 
                 # shutdown will attempt a level switch and execution of 'kill' 
                 # scripts.
                 #
                 /bin/sync
                 /sbin/reboot -f
                 
                 exit
                 ;;
             
            99) 
                 # This is bad. It indicates that during the restore of the 
                 # backup data there was a 'tar' error during processing.  The 
                 # condition *may* now exist on the system where we have 
                 # incompatible executables and libraries, but maybe not - no 
                 # way to tell.  Prompt user with continuation options.

                 echo -e "\n\n\n"
                 echo -e $ARCHIVE_ERR_MSG_0
                 echo -e $ARCHIVE_ERR_MSG_1
                 echo -e $ARCHIVE_ERR_MSG_2
                 echo -e $ARCHIVE_ERR_MSG_3
                 echo -e $ARCHIVE_ERR_MSG_4
                 echo -e $ARCHIVE_ERR_MSG_5
                 echo -e $ARCHIVE_ERR_MSG_6
             
                 # Hold processing until user acknowledges msg
                 # line is /dev/pts/2
                 line=$(tty)
                 while read input;
                 do
                      # <Enter> key appears to get us out of loop
                      break
                 done
             
                 #
                 # Users assumes all risks with continuing with the boot
                 # process. They have been warned of potential instability.
                 #
                 if [[ "$input" == "R" ||  "$input" == "r" ]]; then
                      #
                      # Prompt with additional message to re-install product
                      #
                      echo -e $REINSTALL_MSG
                  
                      # Hold processing until user acknowledges msg
                      line=$(tty)
                      while read input;
                      do
                           # <Enter> key appears to get us out of loop
                           break
                      done
                  
                      # Reboot assuming the recovery CD has been re-inserted
                      /sbin/reboot -f
             
                      exit
                 else
                      # User has been warned.  Force the reboot
                      /bin/sync
                      /sbin/reboot -f
             
                      exit
                 fi
                 ;;
             
            1|2|4|5|6|8|9|10|11|13|14|15) 
                 # Come to think of it now, we should indicate a non-critcial 
                 # error occurred ('mount' failure, media error, no data, etc.) 
                 # and no restore was done. Again, direct them to the error log 
                 # and present continuation options.

                 echo -e "\n\n\n"
                 echo -e $NO_RESTORE_MSG_0
                 echo -e $NO_RESTORE_MSG_1
                 echo -e $NO_RESTORE_MSG_2
                 echo -e $NO_RESTORE_MSG_3
                  
                 # Hold processing until user acknowledges msg
                 line=$(tty)
                 while read input;
                 do
                      # <Enter> key appears to get us out of loop
                      break
                 done
                 ;;
            *) 
                 ;;
        esac

        
        # Now do the restore of upgrade data files, if required
        /bin/sh /opt/hsc/bin/restoreUpgradeFiles 2>/dev/null
        echo "system restore of upgrade files completed, rc = $?." >> $LOG
        
        
        #-----------------------------------------------------------------------
        # After data restore (or not), we should now have the ccfw information 
        # from both the /etc/passwd and /etc/shadow files preserved.  Restore
        # both of them to variables.
        #-----------------------------------------------------------------------
        ccfwprexist=`cat /tmp/ccfw.data`
        echo "data retrieved from ccfw archive file is: <$ccfwpreexist>" >> $LOG
        rm -f /tmp/ccfw.data
        
        
        #-----------------------------------------------------------------------
        # After data has been restored, re-check for ccfw userID
        #-----------------------------------------------------------------------
        typeset -i ccfwpostexist
        ccfwpostexist=0
        userList=`cat /etc/passwd | cut -d':' -f 1`
        for j in $userList
        do
             if [ "$j" == "ccfw" ]; then
                  ccfwpostexist=1
             fi
        done
        
        if [ $ccfwpostexist -eq 0 ]; then
             echo "ccfw userID entry does not exist in /etc/passwd file after data restore" >> $LOG
        else
             echo "ccfw userID entry still exists in /etc/passwd file after data restore" >> $LOG
        fi
        
        
        #-----------------------------------------------------------------------
        # Now that the system data has been restored, check for a specific
        # migration release - to release 3.2.  Since the 'ccfw' userid is new
        # to this particular release, it will not have had a prior password/home
        # directory, etc. Re-add it's entries to /etc/passwd and /etc/shadow
        # files if it does not already exist.
        #-----------------------------------------------------------------------
        typeset -i pv1
        typeset -i pv2
        typeset -i pr
        typeset -i v1
        typeset -i v2
        typeset -i r
        pv1=0
        pv2=0
        pr=0

        # HMC command to get the current release/version        
        x=`/bin/bash /opt/hsc/bin/hsc version`
        
        r=`echo "$x"|grep Release|cut -d ':' -f 2 | cut -d ' ' -f 2`
        v1=`echo "$x"|grep Version|cut -d ':' -f 2 | cut -d ' ' -f 2 | cut -d '.' -f 1`
        v2=`echo "$x"|grep Version|cut -d ':' -f 2 | cut -d ' ' -f 2 | cut -d '.' -f 2`
        if [[ $pv1 -eq 0  ||  $v1 -gt $pv1 ]]; then
             pv1=v1
        fi
        if [[ $pv2 -eq 0  ||  $v2 -gt $pv2 ]]; then
             pv2=v2
        fi 
        if [[ $pr -eq 0  ||  $r -gt $pr ]]; then
             pr=r
        fi
        echo "current HMC release/version is: release $pr, version $pv1.$pv2" >> $LOG
        
        
        #-----------------------------------------------------------------------
        # Re-generate the 'ccfw' userid if and only if release 3.20 AND 'ccfw'
        # userid existed before the restore of data AND 'ccfw' entry cannot be
        # found after data restore
        #-----------------------------------------------------------------------
        if [[ $pr -eq 3 && $pv1 -eq 2 && pv2 -eq 0 && $ccfwpreexist -eq 1 && $ccfwpostexist -eq 0 ]]; then
             echo "about to delete ccfw userid..." >> $LOG
             /usr/sbin/userdel ccfw >> $LOG 2>&1
             
             echo "about to add ccfw userid..." >> $LOG
             /usr/sbin/useradd -c "Common Console Framework" -M -d /opt/ccfw ccfw >> $LOG 2>&1
             CCFWRC=$?
             echo "initial 'useradd' cmd returned: $CCFWRC" >> $LOG
             
             if [ $CCFWRC -ne 0 ]; then
                  # retry with group information
                  /usr/sbin/useradd -c "Common Console Framework" -M -g ccfw -d /opt/ccfw ccfw >> $LOG 2>&1
                  echo "'useradd' cmd with group info returned: $?" >> $LOG
             fi
        fi
        
        
        #-----------------------------------------------------------------------
        # Migrate password
        #-----------------------------------------------------------------------
        export LD_LIBRARY_PATH=/usr/lib:/opt/hsc/lib:/opt/hsc/lib/hmcjni:$LD_LIBRARY_PATH
        CLASSPATH=/usr/websm/codebase/pluginjars/ccfw.jar:/usr/websm/codebase/pluginjars/auifw.jar:/opt/hsc/:/usr/websm/codebase/pluginjars/sniacimom.jar:/usr/websm/codebase/pluginjars/xerces.jar:/usr/websm/codebase/wsm.jar:/usr/websm/codebase/pluginjars/hsc.jar:$CLASSPATH
	export CLASSPATH
	export PATH=/opt/IBMJava2-131/jre/bin:$PATH
	java com.ibm.hsc.common.util.HscPasswordMgr hscroot
        echo "completed hscroot password migration." >> $LOG

        #-----------------------------------------------------------------------
        # Now restore the correct OS version dependent version of the
        # network-functions file
        #-----------------------------------------------------------------------
        if [ -f /tmp/network-functions ]; then
          /bin/cp -f /tmp/network-functions /etc/sysconfig/network-scripts/network-functions
           echo "network-functions script restored." >> $LOG
        fi
        
        #-----------------------------------------------------------------------
        # Reset the hostname since a change could have occurred after doing
        # a restore or restore of upgrade data
        #-----------------------------------------------------------------------
        HOSTNAME=`/bin/hostname`
        echo "current hostname is: $HOSTNAME." >> $LOG

        # Re-read in config data, set environment
        if [ -f /etc/sysconfig/network ]; then
           . /etc/sysconfig/network
        fi

        if [ -z "$HOSTNAME" -o "$HOSTNAME" = "(none)" ]; then
           HOSTNAME=localhost
        fi

        # Reset the hostname
        action $"Resetting hostname to ${HOSTNAME}: " hostname ${HOSTNAME}
        echo "new hostname is: $HOSTNAME." >> $LOG
        
        
        
        #-----------------------------------------------------------------------
        # Now auto-setup if a second Ethernet controller is detected by Kudzu
        #-----------------------------------------------------------------------
        
        #-----------------------------------------------------------------------
        # Query the number of ethernet controllers in the system. We will
        # have at least 1 (integrated) controller and possibly an additional
        # adapter card as well.
        #-----------------------------------------------------------------------
        ADAPTERS=`grep "Ethernet controller:" /proc/pci | wc -l`
        echo "Number of ethernet controllers detected in system is: $ADAPTERS" >> $LOG
        
        if [ $ADAPTERS -gt 1 ]; then
           echo "more than 1 ethernet controller detected..." >> $LOG
        
           # See if we have both ifcfg-ethX files defined
           if [ -f /etc/sysconfig/network-scripts/ifcfg-eth0 ]; then
              echo "ifcfg-eth0 file detected - no changes required" >> $LOG
        
           else
              echo "ifcfg-eth0 file was NOT detected - creating a dummy file" >> $LOG
        
              # This should never happen, but create an ifcfg-eth0 file
              echo "DEVICE=eth0" > /etc/sysconfig/network-scripts/ifcfg-eth0
              echo "BOOTPROTO=none" >> /etc/sysconfig/network-scripts/ifcfg-eth0
              echo "ONBOOT=yes" >> /etc/sysconfig/network-scripts/ifcfg-eth0
              chmod 600 /etc/sysconfig/network-scripts/ifcfg-eth0
           fi
        
           if [ -f /etc/sysconfig/network-scripts/ifcfg-eth1 ]; then
              echo "ifcfg-eth1 file detected - no changes required" >> $LOG
        
           else
              echo "ifcfg-eth1 file was NOT detected - creating a dummy file" >> $LOG
        
              # This should never happen, but create an ifcfg-eth0 file
              echo "DEVICE=eth1" > /etc/sysconfig/network-scripts/ifcfg-eth1
              echo "BOOTPROTO=none" >> /etc/sysconfig/network-scripts/ifcfg-eth1
              echo "ONBOOT=yes" >> /etc/sysconfig/network-scripts/ifcfg-eth1
              chmod 600 /etc/sysconfig/network-scripts/ifcfg-eth1
           fi
        
        else
           echo "only 1 (or no) Ethernet controllers detected" >> $LOG
        
           if [ -f /etc/sysconfig/network-scripts/ifcfg-eth0 ]; then
              echo "ifcfg-eth0 file detected - no changes required" >> $LOG
        
           else
              echo "ifcfg-eth0 file was NOT detected - creating a dummy file" >> $LOG
        
              # This should never happen, but create an ifcfg-eth0 file
              echo "DEVICE=eth0" > /etc/sysconfig/network-scripts/ifcfg-eth0
              echo "BOOTPROTO=none" >> /etc/sysconfig/network-scripts/ifcfg-eth0
              echo "ONBOOT=yes" >> /etc/sysconfig/network-scripts/ifcfg-eth0
              chmod 600 /etc/sysconfig/network-scripts/ifcfg-eth0
           fi
        fi
        
        echo "--------- end log --------" >> $LOG
        
        # finish log file processing
        if [ -f $LOGDIR/ethernet.log ]; then
           # remove prior log file
           rm -f $LOGDIR/ethernet.log
        fi
        chmod 666 $LOG
        mv -f $LOG $FINALLOG
        
              
        #-----------------------------------------------------------------------
        # Add httpd entry to inittab if not already there
        #-----------------------------------------------------------------------
        grep httpd /etc/inittab
        if [ "$?" -eq "1" ]; then
           echo "apac:2345:once:/etc/rc.d/init.d/httpd start" >> /etc/inittab
           
           # If it had to be added to inittab, it has to be started manually
           /etc/rc.d/init.d/httpd start
        fi
                
        	;;
         stop)
         	;;
         *)
         	echo "Usage: hmcRestore {start|stop}"
        	exit 1
        	;;
        esac
exit 0

